﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public class XuLyTaiKhoan
    {
        // ham ma hoa chuoi data theo dang MD5
        //public static byte[] MaHoaMD5(string data)
        //{
        //    System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
        //    byte[] hashedBytes;
        //    System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
        //    hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(data));
        //    return hashedBytes;
        //}

        // tim tai khoan theo ten dang nhap
        public static CTAIKHOAN dangNhap(string username,string password)
        {
            CTAIKHOAN tk = null;
            DAO.CDataProvider reader = new DAO.CDataProvider();            
            String sql = " SELECT * FROM TAIKHOAN ";            

            try
            {
                DataTable dt = reader.DocDuLieu(sql);
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        string username1 = dr["Username"].ToString();
                        string password1= dr["Password"].ToString();
                        if (username1.Equals(username) && password1.Equals(password))
                        {
                            tk = new CTAIKHOAN();
                            tk.Username = dr["Username"].ToString();
                            tk.Password = dr["Password"].ToString();
                            tk.MaNhanVien = int.Parse(dr["MaNhanVien"].ToString());                            
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return tk;
        }
    }
}
